package com.bestow.loan.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.bestow.loan.domain.InvPurchaseRollOut;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bestow.common.annotation.Log;
import com.bestow.common.core.controller.BaseController;
import com.bestow.common.core.domain.AjaxResult;
import com.bestow.common.enums.BusinessType;
import com.bestow.loan.domain.InvFinancePurchase;
import com.bestow.loan.service.IInvFinancePurchaseService;
import com.bestow.common.utils.poi.ExcelUtil;
import com.bestow.common.core.page.TableDataInfo;

/**
 * 申购/赎回管理Controller
 * 
 * @author liliu
 * @date 2022-09-04
 */
@RestController
@RequestMapping("/loan/financePurchase")
public class InvFinancePurchaseController extends BaseController
{
    @Autowired
    private IInvFinancePurchaseService invFinancePurchaseService;

    /**
     * 查询申购/赎回管理列表
     */
    @PreAuthorize("@ss.hasPermi('loan:financePurchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(InvFinancePurchase invFinancePurchase)
    {
        startPage();
        List<InvFinancePurchase> list = invFinancePurchaseService.selectInvFinancePurchaseList(invFinancePurchase);
        return getDataTable(list);
    }

    /**
     * 导出申购/赎回管理列表
     */
    @PreAuthorize("@ss.hasPermi('loan:financePurchase:export')")
    @Log(title = "申购/赎回管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvFinancePurchase invFinancePurchase)
    {
        List<InvFinancePurchase> list = invFinancePurchaseService.selectInvFinancePurchaseList(invFinancePurchase);
        ExcelUtil<InvFinancePurchase> util = new ExcelUtil<InvFinancePurchase>(InvFinancePurchase.class);
        util.exportExcel(response, list, "申购/赎回管理数据");
    }

    /**
     * 获取申购/赎回管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('loan:financePurchase:query')")
    @GetMapping(value = "/{financePurchaseId}")
    public AjaxResult getInfo(@PathVariable("financePurchaseId") Long financePurchaseId)
    {
        return AjaxResult.success(invFinancePurchaseService.selectInvFinancePurchaseByFinancePurchaseId(financePurchaseId));
    }

    /**
     * 新增申购/赎回管理
     */
    @PreAuthorize("@ss.hasPermi('loan:financePurchase:add')")
    @Log(title = "申购/赎回管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InvFinancePurchase invFinancePurchase)
    {
        return toAjax(invFinancePurchaseService.insertInvFinancePurchase(invFinancePurchase));
    }

    /**
     * 修改申购/赎回管理
     */
    @PreAuthorize("@ss.hasPermi('loan:financePurchase:edit')")
    @Log(title = "申购/赎回管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InvFinancePurchase invFinancePurchase)
    {
        return toAjax(invFinancePurchaseService.updateInvFinancePurchase(invFinancePurchase));
    }

    /**
     * 删除申购/赎回管理
     */
    @PreAuthorize("@ss.hasPermi('loan:financePurchase:remove')")
    @Log(title = "申购/赎回管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{financePurchaseIds}")
    public AjaxResult remove(@PathVariable Long[] financePurchaseIds)
    {
        return toAjax(invFinancePurchaseService.deleteInvFinancePurchaseByFinancePurchaseIds(financePurchaseIds));
    }

    /**
     * 详细页面保存，涉及主子表
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody JSONObject jsonObject){
        InvFinancePurchase invFinancePurchase = jsonObject.toJavaObject(InvFinancePurchase.class);
        List<InvPurchaseRollOut> invPurchaseRollOutList = jsonObject.getJSONArray("invPurchaseRollOutList").toJavaList(InvPurchaseRollOut.class);
        return toAjax(invFinancePurchaseService.saveInvFinancePurchase(invFinancePurchase,invPurchaseRollOutList));
    }
}
